package com.aqadoos;

import com.aqadoos.webservice.User;
import com.aqadoos.webservice.UserWebService;
import com.aqadoos.webservice.UserWebServiceImplService;
import flexjson.JSONSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * Sample controller for going to the home page with a message
 */
@Controller
public class UserController {

	private static final Logger logger = LoggerFactory
			.getLogger(UserController.class);


    @Autowired
    private JavaMailSender mailSender;

	/**
	 * Selects the home page and populates the model with a message
	 */
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public String home(Model model) {
		logger.info("Welcome home!");
		//model.addAttribute("");
		return "user";
	}

    @RequestMapping(value="/user/createUser", method=RequestMethod.POST)
    public @ResponseBody String createUser(@RequestParam String name,
                                           @RequestParam String userId,
                                           @RequestParam String password) {
        UserWebServiceImplService serviceImpl = new UserWebServiceImplService();
        UserWebService userWebService = serviceImpl.getUserWebServiceImplPort();        

        User webServiceUser = new User();
        webServiceUser.setName(name);
        webServiceUser.setPassword(password);
        webServiceUser.setUserId(userId);

        userWebService.create(webServiceUser);
                
        return "{result:'success'}";        
    }
	
	@RequestMapping(value="/user/getAllUsers", method=RequestMethod.GET)
	public @ResponseBody String loadUsers() {
		UserWebServiceImplService serviceImpl = new UserWebServiceImplService();
		UserWebService userWebService = serviceImpl.getUserWebServiceImplPort();                        

		List<User> foundUsers = userWebService.findAll();
		logger.info("Loading Users!");
        JSONSerializer jsonSerializer = new JSONSerializer();
		
		return jsonSerializer.serialize(foundUsers);
	}

    // TODO: Use AOP to inject email message
    private void sendCreateUserEmail() {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {

            public void prepare(MimeMessage mimeMessage) throws Exception {

                mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("ata-ul.qadoos@travelex.com"));
                mimeMessage.setFrom(new InternetAddress("mail@mycompany.com"));
                mimeMessage.setText("Testing");
                mimeMessage.setSubject("Hello - There!");
            }
        };

        try {
            this.mailSender.send(preparator);
        }
        catch (MailException ex) {
            ex.printStackTrace();
        }
    }
}
